import axios from 'axios'
import { getBaseUrl, getHeaders, getToken, WHITELIST } from './system'
const request = axios.create({
  baseURL: getBaseUrl(),
  timeout: 5000,
  headers: getHeaders()
})
// //请求拦截
request.interceptors.request.use(
  (config: any) => {
    if (!WHITELIST.includes(config.url)) {
      config.headers.Authorization = getToken()
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// //相应拦截
request.interceptors.response.use(
  (response: any) => {
    if (response.data?.code === 403) {
      console.error('登录过期,请重新登陆')
      return response?.data
    }
    if (response.data?.code !== 200) {
      console.error(response.data?.msg)
      return response?.data
    }
    return response?.data
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default request
